﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Notes.Resources;
using NotesAgent;

namespace Notes
{
    public partial class NewAccountPage : PhoneApplicationPage
    {
        bool working = false;
        public NewAccountPage()
        {
            InitializeComponent();
        }

        private void CancelUpdate_Button_Click(object sender, RoutedEventArgs e)
        {
            ToggleOverlay(false);
        }
        public void OnError(string message)
        {
            Dispatcher.BeginInvoke(() =>
            {

                Password_Area.IsEnabled = true;
                Username_Area.IsEnabled = true;
                working = false;
                ToggleOverlay(false);
                working = false;
                DisableProgressBar();
                MessageBox.Show(message);

            });
        }
        public void ToggleOverlay(bool isOpen)
        {
            working = isOpen;
            if (isOpen)
            {
                //PageTitle.Text = Strings.Working;
                //Current_Step_Label.Visibility = System.Windows.Visibility.Visible;
                CancelUpdate_Button.Visibility = System.Windows.Visibility.Visible;
                RemainingTime_Label.Visibility = System.Windows.Visibility.Visible;
                EnableProgressBar();

                CheckUserPassword_Button.Visibility = System.Windows.Visibility.Collapsed;

                Username_Text.Visibility = System.Windows.Visibility.Collapsed;
                Username_Area.Visibility = System.Windows.Visibility.Collapsed;
                Password_Text.Visibility = System.Windows.Visibility.Collapsed;
                Password_Area.Visibility = System.Windows.Visibility.Collapsed;
                Password2_Text.Visibility = System.Windows.Visibility.Collapsed;
                Password2_Area.Visibility = System.Windows.Visibility.Collapsed;
            }
            else
            {
                DisableProgressBar();
                Username_Text.Visibility = System.Windows.Visibility.Visible;
                Username_Area.Visibility = System.Windows.Visibility.Visible;
                Password_Text.Visibility = System.Windows.Visibility.Visible;
                Password_Area.Visibility = System.Windows.Visibility.Visible;
                Password2_Text.Visibility = System.Windows.Visibility.Visible;
                Password2_Area.Visibility = System.Windows.Visibility.Visible;
                CheckUserPassword_Button.Visibility = System.Windows.Visibility.Visible;
                CancelUpdate_Button.Visibility = System.Windows.Visibility.Collapsed;
            }
            
        }
        private void EnableProgressBar()
        {
            Dispatcher.BeginInvoke(() =>
            {
                ToggleProgressBar.IsEnabled = true;
                ToggleProgressBar.IsIndeterminate = true;
                ToggleProgressBar.Visibility = System.Windows.Visibility.Visible;
            });
        }
        private void DisableProgressBar()
        {
            Dispatcher.BeginInvoke(() =>
            {
                ToggleProgressBar.IsEnabled = false;
                ToggleProgressBar.IsIndeterminate = false;
                ToggleProgressBar.Visibility = System.Windows.Visibility.Collapsed;
            });
        }
        private void CheckUserPassword_Button_Click(object sender, RoutedEventArgs e)
        {
            working = true;

            if (Username_Area.Text == "" || Password_Area.Password == "" || Password2_Area.Password == "")
            {
                OnError(Strings.MissingData);
                return;
            }
            else if (Password_Area.Password != Password2_Area.Password)
            {
                OnError(Strings.SamePasswords);
                return;
            }
            /*else
            {
                var at_pos = Username_Area.Text.IndexOf('@');
                var last_dot = Username_Area.Text.LastIndexOf('.');

                if (at_pos < 0 || at_pos == Username_Area.Text.Length - 1 ||
                   last_dot < 0 || last_dot == Username_Area.Text.Length - 1 ||
                   at_pos >= last_dot
                )
                {
                    OnError(Strings.EmailWrongFormat);
                    return;
                }
            }*/
            ToggleOverlay(true);
            CreateAccount();
        }

        public void CreateAccount()
        {
            string url = "http://fboscari.altervista.org/Notes/save_phone.php?";
            //url += Utils.GetUpdateLiveTileArgs(result, currentAccount.contract, currentAccount.username, currentAccount.password, currentAccount.number, currentAccount.showDate);
            url += "Username=" + Username_Area.Text + "&Pwd=" + Password_Area.Password + "&salt=" + DateTime.Now.Millisecond;
            Storage.getHTML(url, s =>
            {
                Dispatcher.BeginInvoke(() =>
                {
                    ToggleOverlay(false);
                    if (s == "-2")
                    {
                        OnError(Strings.EmailWrongFormat);
                        return;
                    }
                    else if (s == "-1")
                    {
                        OnError(Strings.EmailAlreadyRegistered);
                        return;
                    }
                    else
                    {
                        Account c = new Account();
                        c.username = Username_Area.Text;
                        c.password = Password_Area.Password;
                        Storage.SaveAccount(c);
                        NavigationService.GoBack();
                    }
                    //NavigationService.GoBack();
                });
            },
            (e) =>
            {
                OnError(Strings.ConnectionError);

            });
        }

        private void Password2_Area_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                if (Password2_Area.Password != "")
                    if (Username_Area.Text != "")
                    {
                        CheckUserPassword_Button.Focus();
                        CheckUserPassword_Button_Click(null, null);
                    }
                    else
                        Username_Area.Focus();
            }
        }

        private void Password_Area_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                if (Password_Area.Password != "")
                    if (Password2_Area.Password != "")
                    {
                        CheckUserPassword_Button.Focus();
                        CheckUserPassword_Button_Click(null, null);
                    }
                    else
                        Password2_Area.Focus();
            }
        }

        private void Username_Area_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                if (Username_Area.Text != "")
                    if (Password_Area.Password != "")
                    {
                        CheckUserPassword_Button.Focus();
                        CheckUserPassword_Button_Click(null, null);

                    }
                    else
                        Password_Area.Focus();
            }
        }
    }
}